package com.lines;

import java.util.List;

import com.lines.Game;
import com.lines.Graphics;
import com.lines.Screen;
import com.lines.Input.TouchEvent;

public class MainMenuScreen extends Screen {

	static final float TICK_INITIAL = 0.4f;
	float tickTime = 0;
	int curFrame = 0;
	
	public MainMenuScreen(Game game) {
		super(game);
	}

	@Override
	public void update(float deltaTime) {
		Graphics g = game.getGraphics();
		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
		game.getInput().getKeyEvents();
		int len = touchEvents.size();
		for(int i = 0; i < len; i++) {
			TouchEvent event = touchEvents.get(i);
			if(event.type == TouchEvent.TOUCH_UP) {
				if(inBounds(event, 0, g.getHeight() - 64, 64, 64)) {
					Settings.soundEnabled = !Settings.soundEnabled;
					if(Settings.soundEnabled)
						Assets.click.play(1);
				}
				if(inBounds(event, 64, 220, 192, 60) ) {
					game.setScreen(new GameScreen(game));
					if(Settings.soundEnabled)
						Assets.click.play(1);
					return;
				}
				if(inBounds(event, 64, 220 + 60, 192, 60) ) {
					game.setScreen(new HighscoreScreen(game));
					if(Settings.soundEnabled)
						Assets.click.play(1);
					return;
				}
			}
		}
}


	private boolean inBounds(TouchEvent event, int x, int y, int width, int height) {
		if(event.x > x && event.x < x + width - 1 &&
				event.y > y && event.y < y + height - 1)
			return true;
		else
			return false;
	}

	@Override
	public void present(float deltaTime) {
		Graphics g = game.getGraphics();
		g.drawPixmap(Assets.background, 0, 0);
		g.drawPixmap(Assets.logo, 32, 20);
		g.drawPixmap(Assets.play, 64, 220, 0, curFrame*60, 192, 60);
		g.drawPixmap(Assets.hs, 64, 280, 0, curFrame*60, 192, 60);
		if(Settings.soundEnabled)
			g.drawPixmap(Assets.buttons, 0, 416, 0, 0, 64, 64);
		else
			g.drawPixmap(Assets.buttons, 0, 416, 64, 0, 64, 64);
		
		tickTime += deltaTime;
		while (tickTime > TICK_INITIAL) {
			tickTime -= TICK_INITIAL;
			curFrame++;
			if(curFrame > 4) curFrame = 0;
		}
	}

	@Override
	public void pause() {
		Settings.save(game.getFileIO());
	}

	@Override
	public void resume() {
	}
	
	@Override
	public void dispose() {
	}
}

